﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNNspot.LiveChat.DataModel
{
    public partial class Agent
    {
        public static Agent Load(int? agentId)
        {
            var agent = new Agent();

            if (agentId != null)
                agent.LoadByPrimaryKey(Convert.ToInt32(agentId));

            return agent;
        }

        public static Agent LoadByUserId(int userId)
        {
            var q = new AgentQuery();
            q.Where(q.UserId == userId);

            var agent = new Agent();
            agent.Load(q);

            return agent;
        }

        public static bool IsAgent(int userId)
        {
            var q = new AgentQuery();
            q.Where(q.UserId == userId);

            var agents = new AgentCollection();
            agents.Load(q);

            return agents.Count > 0;
        }
    }

    public partial class AgentCollection
    {
        public static bool AgentIsAvailable()
        {
            var q = new AgentQuery();
            q.Where(q.IsLoggedIn == true);

            var agents = new AgentCollection();
            agents.Load(q);

            return agents.Count > 0;
        }

        public static int GetTotal(FlexigridSearchCriteria searchCriteria)
        {
            var tmpNumber = searchCriteria.PageNumber;
            var tmpSize = searchCriteria.PageSize;

            searchCriteria.PageNumber = -1;
            searchCriteria.PageSize = -1;

            var count = Search(searchCriteria).Count;

            searchCriteria.PageNumber = tmpNumber;
            searchCriteria.PageSize = tmpSize;

            return count;
        }

        public static void RemoveAllInactive(int seconds)
        {
            var maxDate = DateTime.Now.AddSeconds(-seconds);
            var q = new AgentQuery();
            q.Where(q.ModifiedDate < maxDate);

            var agents = new AgentCollection();
            
            if(agents.Load(q))
                foreach (Agent agent in agents)
                    agent.IsLoggedIn = false;

            agents.Save();
            
        }

        public static List<Agent> Search(FlexigridSearchCriteria searchCriteria)
        {
            var agentQuery = new AgentQuery("a");
            var userQuery = new UserQuery("u");

            agentQuery.es.PageNumber = searchCriteria.PageNumber;
            agentQuery.es.PageSize = searchCriteria.PageSize;

            agentQuery.Select(userQuery.FirstName, userQuery.LastName, agentQuery);
            agentQuery.InnerJoin(userQuery).On(userQuery.UserID == agentQuery.UserId);
            agentQuery.Where(agentQuery.PortalId == searchCriteria.PortalId);

            foreach (String keyword in searchCriteria.Query.Split(' '))
            {
                agentQuery.And(string.Format("{0} LIKE '%{1}%'", searchCriteria.QueryColumns, keyword));
            }

            agentQuery.OrderBy(searchCriteria.SortColumn, searchCriteria.SortDirection);

            var agents = new AgentCollection();
            agents.Load(agentQuery);

            return agents;
        }
    }
}
